package br.com.examCertification;

import java.util.ArrayList;
import java.util.List;

import br.com.examCertification.dao.DataManager;
import br.com.examCertification.model.Pergunta;
import br.com.examCertification.model.TipoCertificacao;
import br.com.examCertification.model.TipoExame;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class PreStartExame extends Activity {
	
	private DataManager dataManager;
	
	EditText numeroDePerguntas;

	Button iniciar;
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prestartexame);
        dataManager = DataManagerFactory.getInstance(this);
        
        numeroDePerguntas = (EditText) findViewById(R.id.editText1);
        
        Bundle extras = getIntent().getExtras();         
        
        final List<Pergunta> perguntas = (List<Pergunta>) dataManager.getPerguntasById(extras.getLong("id"));  
        
        iniciar = (Button) findViewById(R.id.iniciar);
        iniciar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				 gerarPerguntas(perguntas);
					Intent i = new Intent(PreStartExame.this,  RedePergunta.class);                  
	                Bundle extras = new Bundle();
	                SinglePerguntas.perguntas = gerarPerguntas(perguntas);
	                extras.putIntegerArrayList("id", gerarPerguntas(perguntas));
	                i.putExtras(extras);
	                startActivity(i);
			}
		});
        
     }
    
    public ArrayList<Integer> gerarPerguntas(List<Pergunta> perguntas){
    	
	    List<Integer> perguntasParaTela = new ArrayList<Integer>();
	    
	    int quantidadesDePerguntas = Integer.parseInt(numeroDePerguntas.getText().toString());
	   
	    for(int i = 0; i < perguntas.size() ; i++){
	    	int x = (int)(quantidadesDePerguntas * Math.random()) + 1;
	    	
	    	perguntasParaTela.add((int) perguntas.get(i).getId());
	    	
	    	if(perguntasParaTela.size() >= quantidadesDePerguntas){
	    		break;
	    	}
	    	
	    }
    	
	    return (ArrayList<Integer>) perguntasParaTela;
    }
}
 